<template>
  <div id="map" class="map"></div>
</template>

<script lang="ts" setup>
import KML from 'ol/format/KML'
import Map from 'ol/Map'
import VectorSource from 'ol/source/Vector'
import View from 'ol/View'
import XYZ from 'ol/source/XYZ'
import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer'
import { onMounted, onBeforeUnmount } from 'vue'
import { MAPURL, ATTRIBUTIONS } from '../../constants'

const raster = new TileLayer({
  source: new XYZ({
    attributions: ATTRIBUTIONS,
    url: MAPURL,
    maxZoom: 20
  })
})

const vector = new VectorLayer({
  source: new VectorSource({
    url: '/kml/2022.kml',
    format: new KML()
  })
})

let map: Map | null = null

onMounted(() => {
  map = new Map({
    layers: [raster, vector],
    target: 'map',
    view: new View({
      center: [876970.8463461736, 5859807.853963373],
      projection: 'EPSG:3857',
      zoom: 10
    })
  })
})

onBeforeUnmount(() => {
  if (map) {
    map.dispose()
    map = null
  }
})
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
#map {
  width: 100%;
  height: 100%;
}
</style>